package algorithm;

import java.util.HashMap;
import java.util.Map;

public class NumberTest {

    public static void main(String[] args) {
        //int[] arr = {1,1,2,2,3,4,4};
        //System.out.println(findOnce(arr));
        int[] arr = {1,3,5,7,9};
        findTwoSum(arr, 12);
    }

    /**
     * 0011
     * 0011
     * ----
     * 0000
     */
    // 数组中只出现一次的数字,任何数和自己异或值为0
    private static int findOnce(int[] num) {
        int res =0;
        for (int n : num) {
            res ^= n;
        }
        return res;
    }

    private static boolean findTwoSum(int[] arr, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int a : arr) {
            if (map.containsKey(a)) {
                int m = map.get(a);
                return true;
            } else {
                map.put(target-a,a);
            }
        }
        return false;
    }
}
